/*
 *  Copyright 2019-2020 Zheng Jie
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.fzhucc.modules.thinktank.service;

import com.fzhucc.exception.BadRequestException;
import com.fzhucc.modules.thinktank.domain.Academician;
import com.fzhucc.modules.thinktank.domain.Leader;
import com.fzhucc.modules.thinktank.domain.LeaderMsgBox;
import com.fzhucc.modules.thinktank.domain.dto.AcademicianMsgBoxDto;
import com.fzhucc.modules.thinktank.domain.dto.LeaderBoxSeekDto;
import com.fzhucc.modules.thinktank.domain.dto.LeaderMsgBoxDto;
import com.fzhucc.modules.thinktank.domain.dto.LeaderMsgBoxQueryCriteria;
import com.fzhucc.modules.thinktank.repository.LeaderMsgBoxRepository;
import com.fzhucc.modules.thinktank.repository.LeaderRepository;
import com.fzhucc.modules.thinktank.service.mapstruct.LeaderMsgBoxMapper;
import com.fzhucc.utils.FileUtil;
import com.fzhucc.utils.PageUtil;
import com.fzhucc.utils.QueryHelp;
import com.fzhucc.utils.ValidationUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.*;

/**
 * @author yyg
 * @website https://eladmin.vip
 * @description 服务实现
 * @date 2023-03-10
 **/
@Service
@RequiredArgsConstructor
public class LeaderMsgBoxService {

    private final LeaderMsgBoxRepository leaderMsgBoxRepository;

    private final LeaderRepository leaderRepository;
    private final LeaderMsgBoxMapper leaderMsgBoxMapper;

    public Map<String, Object> queryAll(LeaderMsgBoxQueryCriteria criteria, Pageable pageable) {
        Page<LeaderMsgBox> page = leaderMsgBoxRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder), pageable);
        Page<LeaderMsgBoxDto> map = page.map(leaderMsgBoxMapper::toDto);
        List<LeaderMsgBoxDto> list = map.getContent();
        if (list != null) {
            for (LeaderMsgBoxDto leaderMsgBoxDto : list) {
                Optional<Leader> res = leaderRepository.findById(leaderMsgBoxDto.getLeaderId());
                if (res.isPresent()) {
                    leaderMsgBoxDto.setLeader(res.get());
                }
            }
        }
        return PageUtil.toPage(map);
    }

    public List<LeaderMsgBoxDto> queryAll(LeaderMsgBoxQueryCriteria criteria) {
        return leaderMsgBoxMapper.toDto(leaderMsgBoxRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder)));
    }

    public LeaderMsgBoxDto findById(Integer id) {
        LeaderMsgBox leaderMsgBox = leaderMsgBoxRepository.findById(id).orElseGet(LeaderMsgBox::new);
        ValidationUtil.isNull(leaderMsgBox.getId(), "LeaderMsgBox", "id", id);
        return leaderMsgBoxMapper.toDto(leaderMsgBox);
    }

    @Transactional(rollbackFor = Exception.class)
    public LeaderMsgBoxDto create(LeaderMsgBox resources) {
        return leaderMsgBoxMapper.toDto(leaderMsgBoxRepository.save(resources));
    }

    @Transactional(rollbackFor = Exception.class)
    public void update(LeaderMsgBox resources) {
        LeaderMsgBox leaderMsgBox = leaderMsgBoxRepository.findById(resources.getId()).orElseGet(LeaderMsgBox::new);
        ValidationUtil.isNull(leaderMsgBox.getId(), "LeaderMsgBox", "id", resources.getId());
        leaderMsgBox.copy(resources);
        leaderMsgBoxRepository.save(leaderMsgBox);
    }

    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            leaderMsgBoxRepository.deleteById(id);
        }
    }

    public void download(List<LeaderMsgBoxDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (LeaderMsgBoxDto leaderMsgBox : all) {
            Map<String, Object> map = new LinkedHashMap<>();
            map.put("产业龙头id", leaderMsgBox.getLeaderId());
            map.put("咨询内容", leaderMsgBox.getContent());
            map.put("联系方式", leaderMsgBox.getPhone());
            map.put("咨询时间", leaderMsgBox.getConsultationTime());
            map.put("咨询人姓名", leaderMsgBox.getName());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    public LeaderMsgBoxDto seek(LeaderBoxSeekDto resources) {
        LeaderMsgBox leaderMsgBox = new LeaderMsgBox();
        Optional<Leader> res = leaderRepository.findById(resources.getLeaderId());
        if (!res.isPresent()) {
            throw new BadRequestException("未找到该产业龙头");
        }
        leaderMsgBox.setLeaderId(resources.getLeaderId());
        leaderMsgBox.setPhone(resources.getPhone());
        leaderMsgBox.setConsultationTime(new Timestamp(new Date().getTime()));
        leaderMsgBox.setContent(resources.getContent());
        leaderMsgBox.setName(resources.getName());
        LeaderMsgBox leaderMsgBox1 = leaderMsgBoxRepository.save(leaderMsgBox);
        return leaderMsgBoxMapper.toDto(leaderMsgBox1);
    }
}